package com.odp.service.impl;

import com.odp.entity.Reply;
import com.odp.mapper.CommentMapper;
import com.odp.mapper.PostMapper;
import com.odp.mapper.ReplyMapper;
import com.odp.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ReplyServiceImpl implements ReplyService {
    @Autowired
    private ReplyMapper replyMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private PostMapper postMapper;
    @Override
    public boolean likeReply(int replyId) {
        int flag = replyMapper.likeReply(replyId);
        if(flag>0)
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean makeReply(Reply reply) {
        //1.回复表中新建表项
        //前端传帖子id 评论id 被回复的人的id（楼层层主id） content
        //当前登录的用户id（session中取）
        reply.setUserId(2);
        reply.setCreateTime(new Date());
        int flag1 = replyMapper.makeReply(reply);
        if(flag1==0){
            return false;
        }
        //2.楼层表中 对应表项  未读数量+1
        int flag2 = commentMapper.reply4Comment(reply.getCommentId());
        if(flag2==0){
            return false;
        }
        //3.帖子表中 对应表项 未读数量+1  回复数+1
        int flag3 = postMapper.reply4Post(reply.getPostId());
        if(flag3==0){
            return false;
        }
        return true;
    }
}
